Next
For <.counterVar.> = <.initialValue.> To <.finalValue.> [Step <.stepValue.>] ... Next [counterVar]
 
Parameters: NONE
Returns: NONE
 

      A For-Next, unlike a Repeat-Until or While-EndWhile loop, requires you to specify explicitly the number of iterations you want the loop to go through. At the start of the first iteration the initialValue is assigned to the counter variable (integer type). At the end of each iteration the counter is increased by 1, or if an optional Step statement is given, by the stepValue. If the value of the counter variable is greater than the finalValue, PlayBASIC exits the loop. If the initialValue is greater than the finalValue, stepValue should be a negative number, so that the counter will be decreased. In this case PlayBASIC exits the loop if the value of the counter variable is less than finalValue.




FACTS:


      * For-Next statements must be paired.

     * The counter variable may follow the Next statement, but is not necessary.

      * You can exit a For-Next loop anytime with the Exit or the ExitFor statement.




Mini Tutorial:


      Examples of For-Next Loops

  
; A simple For-Next loop
  For i = 1 To 4
     Print i
  Next i
  Sync
  
; A For-Next loop with a Step statement
  For i = 1 To 8 Step 2
     Print i
  Next i
  Sync
  
; A For-Next loop that goes down from 4 to 1
  For i = 4 To 1 Step -1
     Print i
  Next i
  Sync
  
  WaitKey
  


Thise code will output:

  
  1
  2
  3
  4
  1
  3
  5
  7
  4
  3
  2
  1
  

 
Related Info: Continue | Each | Exit | ExitFor | For | List | Loops | Repeat | Step | While :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com